package TUILayer;
import ControlLayer.*;
import  ModelLayer.*;
import java.util.Scanner;


public class Loan
{
    // instance variables - replace the example below with your own
    private LoanCtr loanCtr;
    
    public Loan()
    {
       loanCtr=new LoanCtr();
    }

     public void menuLoan()
    {
       boolean exit=false;
       while (!exit) //! means while exit not is true (that is: false)
       {
           int choise = printLoan();
           switch (choise)
            {
                case 1:
                {
                       createLoan();
                       break;
                }
                case 2:
                {
                       //add person
                       readLoanName();
                       break;
                }
                case 3:
                {
                       //remove person
                       readLoanTitle();
                       break;
                }
                case 4:
                {
                    //Update person
                    returnLoan();
                    break;
                }
                case 5:
                {
                    //list all persons
                    deleteLoan();
                    break;
                }
                case 6:
                {
                    //return
                    exit = true;        
                    break;
                }
            
            }
        }
    }
    
    private int printLoan()
    {
       Scanner keyboard = new Scanner(System.in);
       System.out.println("\f *** Laon ***");
       System.out.println(" (1)Create Loan");
       System.out.println(" (2)Read loan informationf from name:");
       System.out.println(" (3)Read loan informationf from title:"); 
       System.out.println(" (4)Return loan");
       System.out.println(" (5)Delete loan");
       System.out.println(" (6)Return"); 
       System.out.print("\n Make your choise: ");
       int choise = keyboard.nextInt();
       return choise;
    }
    
    private void createLoan()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Person name:");
        String name=keyboard.nextLine();
        if(loanCtr.checkPerson(name)!=null)
        {   
            System.out.println("Title");
            String title=keyboard.nextLine();
            System.out.println("Serial number");
            int serialNumber=keyboard.nextInt();
            if(loanCtr.checkTitle(title,serialNumber)!=null)
            {  
                 System.out.println("Id:");
                 int id=keyboard.nextInt();
                 System.out.println("BorrowDate:");
                 keyboard.nextLine();
                 String borrowDate=keyboard.nextLine();
                 System.out.println("Period:");
                 String period=keyboard.nextLine();
                 loanCtr.createLoan(loanCtr.checkPerson(name),loanCtr.checkTitle(title,serialNumber), name, title, id, borrowDate, period);
                 System.out.println("Select (1) to continue!");
                 boolean exit=false;
                 while (!exit)
                 {
                     int select=keyboard.nextInt();
                     if(select==1)
                    exit=true;
                 }  
            }
            else
            {
                System.out.println("DVD Title not in the sistem or there is no available copy!");
                System.out.println("Select (1) to continue and try again!");
                boolean exit=false;
                while (!exit)
                {
                    int select=keyboard.nextInt();
                    if(select==1)
                    exit=true;
                }
            }
        }
            else
            {
                System.out.println("Name not in the sistem! Select (1) to continue and try again!");
                boolean exit=false;
                while (!exit)
                {
                    int select=keyboard.nextInt();
                    if(select==1)
                    exit=true;
                }
            }
        }
        
        private void readLoanName()
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Person name:");
            String name=keyboard.nextLine();
            if(loanCtr.checkPerson(name)!=null)
            {
                loanCtr.readLoanName(name);
                System.out.println("Select (1) to continue!");
                boolean exit=false;
                while (!exit)
                {
                    int select=keyboard.nextInt();
                    if(select==1)
                    exit=true;
                }
            }
            else
            {
                System.out.println("Name not in the sistem! Select (1) to continue and try again!");
                boolean exit=false;
                while (!exit)
                {
                    int select=keyboard.nextInt();
                    if(select==1)
                    exit=true;
                }
            }
        }
       
        private void readLoanTitle()
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("DVD title:");
            String title=keyboard.nextLine();loanCtr.readLoanTitle(title);
            System.out.println("Select (1) to continue!");
            boolean exit=false;
            while (!exit)
            {
                int select=keyboard.nextInt();
                if(select==1)
                exit=true;
            }
        }
        
        private void returnLoan()
        { 
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Person name:");
            String name=keyboard.nextLine();
            if(loanCtr.checkPerson(name)!=null)
            {   
                System.out.println("Title");
                String title=keyboard.nextLine();
                System.out.println("ID");
                int id=keyboard.nextInt();
                keyboard.nextLine();
                loanCtr.returnLoan(name,title);
                System.out.println("Select (1) to continue!");
                boolean exit=false;
                while (!exit)
                {
                    int select=keyboard.nextInt();
                    if(select==1)
                    exit=true;
                }
            }
            else
            {
                System.out.println("Name not in the sistem! Select (1) to continue and try again!");
                boolean exit=false;
                while (!exit)
                {
                    int select=keyboard.nextInt();
                    if(select==1)
                    exit=true;
                }
            }
        }
        
        
        
        private void deleteLoan()
        { 
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Person name:");
            String name=keyboard.nextLine();
            if(loanCtr.checkPerson(name)!=null)
            {   
                System.out.println("Title");
                String title=keyboard.nextLine();
                loanCtr.deleteLoan(name,title);
                System.out.println("Select (1) to continue!");
                boolean exit=false;
                while (!exit)
                {
                    int select=keyboard.nextInt();
                    if(select==1)
                    exit=true;
                }
            }
            else
            {
                System.out.println("Name not in the sistem! Select (1) to continue and try again!");
                boolean exit=false;
                while (!exit)
                {
                    int select=keyboard.nextInt();
                    if(select==1)
                    exit=true;
                }
            }
        }
    }

